package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "房间信息管理")
@RestController
@RequestMapping("/admin/room")
public class RoomController {

    @Autowired
    private RoomLeaseTermService roomLeaseTermService ;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomAttrValueService roomAttrValueService ;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private GraphInfoService graphInfoService ;

    @Autowired
    private RoomInfoService roomInfoService ;

    @Operation(summary = "保存或更新房间信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        //1.删除旧关系
        if(id!=null){
            //1-1. 删除租期关系
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
            //1-2. 删除支付方式关系
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
            //1-3. 删除属性值关系
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
            //1-4. 删除标签关系
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
            //1-5. 删除配套关系
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
            //1-6. 删除图片
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId,id));
        }

        //2.添加新的关系
        //2-1. 保存房间信息
        roomInfoService.saveOrUpdate(roomSubmitVo);
        //2-2. 保存新的属性关系
        if(!CollectionUtils.isEmpty(roomSubmitVo.getAttrValueIds())) {
            roomAttrValueService.saveBatch(roomSubmitVo.getAttrValueIds().stream().map(attrValueId ->
                    RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build()
            ).collect(Collectors.toList()));
        }
        //2-3. 保存新的配套关系
        if(!CollectionUtils.isEmpty(roomSubmitVo.getFacilityInfoIds())) {
            roomFacilityService.saveBatch(roomSubmitVo.getFacilityInfoIds().stream().map(facilityId ->
                    RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityId).build()
            ).collect(Collectors.toList()));
        }
        //2-4. 保存新的标签关系
        if(!CollectionUtils.isEmpty(roomSubmitVo.getLabelInfoIds())) {
            roomLabelService.saveBatch(roomSubmitVo.getLabelInfoIds().stream().map(labelId ->
                    RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelId).build()
            ).collect(Collectors.toList()));
        }
        //2-5. 保存新的支付方式
        if(!CollectionUtils.isEmpty(roomSubmitVo.getPaymentTypeIds())) {
            roomPaymentTypeService.saveBatch(roomSubmitVo.getPaymentTypeIds().stream().map(paymentId ->
                    RoomPaymentType.builder().paymentTypeId(paymentId).roomId(roomSubmitVo.getId()).build()
            ).collect(Collectors.toList()));
        }
        //2-6. 保存新的租期关系
        if(!CollectionUtils.isEmpty(roomSubmitVo.getLeaseTermIds())) {
            roomLeaseTermService.saveBatch(roomSubmitVo.getLeaseTermIds().stream().map(leaseTermId ->
                    RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(roomSubmitVo.getId()).build()
            ).collect(Collectors.toList()));
        }
        //2-7. 保存新的图片信息
        if(!CollectionUtils.isEmpty(roomSubmitVo.getGraphVoList())) {
            graphInfoService.saveBatch(roomSubmitVo.getGraphVoList().stream().map(graphVo -> {
                GraphInfo graphInfo = new GraphInfo();
                BeanUtils.copyProperties(graphVo, graphInfo);
                //别忘了设置图片的所属类型：房间类型 以及 ItemId
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                return graphInfo;
            }).collect(Collectors.toList()));
        }

        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询房间列表")
    @GetMapping("pageItem")
    public Result<IPage<RoomItemVo>> pageItem(@RequestParam long current, @RequestParam long size, RoomQueryVo queryVo) {
        IPage<RoomItemVo> roomItemVoPage = roomInfoService.pageItem(Page.of(current,size),queryVo);
        return Result.ok(roomItemVoPage);
    }

    @Operation(summary = "根据id获取房间详细信息")
    @GetMapping("getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam Long id) {
        //RoomDetailVo roomDetailVo = roomInfoService.getDetailById(id);
        RoomDetailVo roomDetailVo = roomInfoService.getDetailById2(id);
        return Result.ok(roomDetailVo);
    }

    @Operation(summary = "根据id删除房间信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {

        //1.删除房间本身信息
        roomInfoService.removeById(id);

        //2.删除房间关系信息
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.ROOM));

        return Result.ok();
    }

    @Operation(summary = "根据id修改房间发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(Long id, ReleaseStatus status) {
        roomInfoService.update(new LambdaUpdateWrapper<RoomInfo>().set(RoomInfo::getIsRelease,status).eq(RoomInfo::getId,id));
        return Result.ok();
    }

    @GetMapping("listBasicByApartmentId")
    @Operation(summary = "根据公寓id查询房间列表")
    public Result<List<RoomInfo>> listBasicByApartmentId(Long id) {
        return Result.ok(roomInfoService.list(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId,id)));
    }

}

/*
ClassBean : 班级
学员:        学员

class Student{
   private ClassBean cb ;  //对一关联
}

class ClassBean{
  private Integer id ;
  private String cname ;
}



*/
/*
<select id="" resultType="Student">
select t1.* , t2.id 'cb.id' , t2.cname as 'cb.cname'  from t_stu t1 inner join t_class t2 on t1.classId = t2.id

<resultMap id="" type="">
 <id>...</id>
 <result property="" column=""/>
 <!-- 对一关联 -->
 <association javaType="">
 <!-- 对多关联 -->
 <collection ofType="">
</resultMap>

 */
















